<?php
class MediaUtility {
	// call constructor
	private function __construct() {}

	/**
	 * Save uploaded media info 
	 * @param $pageVars 
	 * mediaId
	*/
	public static function saveMediaData( $pageVars ){
		try{
			global $eiLoginUserId;
			if( $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_array( $pageVars ) ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			if( count( $pageVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$categoryId1 = isset( $pageVars[ 'categoryId1' ] ) ? $pageVars[ 'categoryId1' ] : 0;
			$categoryId2 = isset( $pageVars[ 'categoryId2' ] ) ? $pageVars[ 'categoryId2' ] : 0;

			// save file data
			$fileName = isset( $pageVars[ 'filename' ] ) ? $pageVars[ 'filename' ] : '';
			$fileName = makeUriTitle( $fileName );
			$filePath = isset( $pageVars[ 'key' ] ) ? $pageVars[ 'key' ] : '';
			$mimetype = isset( $pageVars[ 'mimetype' ] ) ? $pageVars[ 'mimetype' ] : '';
			$tags = isset( $pageVars[ 'tags' ] ) ? $pageVars[ 'tags' ] : '';
			$size = isset( $pageVars[ 'size' ] ) ? $pageVars[ 'size' ] : 0;
			$height = isset( $pageVars[ 'height' ] ) ? $pageVars[ 'height' ] : 0;
			$width = isset( $pageVars[ 'width' ] ) ? $pageVars[ 'width' ] : 0;
			$accessType = isset( $pageVars[ 'accessType' ] ) ? $pageVars[ 'accessType' ] : '';
			$youtubeCategoryId = isset( $pageVars[ 'youtubeCategoryId' ] ) ? $pageVars[ 'youtubeCategoryId' ] : 0;

			$fileMimeTypeArr = explode( '/', $mimetype );

			if( count( $fileMimeTypeArr ) <= 1 ){
				throw new Exception( 'Unspported mimetype!' );
			}
			$type = $fileMimeTypeArr[ 0 ];
			// check every parameters are required parameter			
			if( $fileName == '' || $filePath == '' || $mimetype == '' || $type == '' || $size <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$mediaId = 0;
			if( $type == 'image' ){
				if( $height <= 0 || $width <= 0 ){
					if( $accessType == 'public' ){
						// get image dimensions
						// process the url and get dimentions of image
						$imageInfo = getImageDimension( $filePath );
						$width = $imageInfo['width'];
						$height = $imageInfo['height'];
						if( $width <= 0 || $height <= 0 ){
							throw new Exception( 'No height and width are found for image !' );
						}
					}
				}
				$objImgChk = getMultiple( 'image', "uri||$filePath" );
				if( $objImgChk->getResultCount() > 0 ){
					$mediaId = $objImgChk->getImageId();
				}else{
					// add user's linkedin data
					$mediaId = getDb( 'image', 'add' )->set(
						"title||$fileName", "uri||$filePath", "mimetype||$mimetype",
						"size||$size", "height||$height", "width||$width", 'status||active',
						"categoryId1||$categoryId1", "categoryId2||$categoryId2" )->getLastInsertedId();
				}
			}else{
				$status = 'published';
				if( $type == 'video' || $type == 'audio' ){
					$status = $type == 'video' ? 'pretranscode' : 'preaudiotranscode';
				}else{
					$type = array_pop( explode( '.', $filePath ) );
				}

				// add user's linkedin data
				$mediaId = getDb( 'media', 'add' )->set(
					"title||$fileName", "filePath||$filePath", "mimeType||$mimetype",
					"size||$size", "type||$type", "status||$status", "youtubeCategoryId||$youtubeCategoryId",
					"categoryId1||$categoryId1", "categoryId2||$categoryId2", "tags||$tags" )->getLastInsertedId();
			}
			return $mediaId;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * Save embed or url media info 
	 * @param $pageVars 
	 * mediaId
	*/
	public static function saveEmbedOrUrlLinkData( $pageVars ){
		try{
			global $eiLoginUserId;
			if( $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_array( $pageVars ) ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			if( count( $pageVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}

			$filePath = isset( $pageVars[ 'filePath' ] ) ? $pageVars[ 'filePath' ] : '';
			$socialId = isset( $pageVars[ 'socialId' ] ) ? $pageVars[ 'socialId' ] : '';
			$source = isset( $pageVars[ 'source' ] ) ? $pageVars[ 'source' ] : '';
			$type = isset( $pageVars[ 'type' ] ) ? $pageVars[ 'type' ] : '';
			if( $filePath == '' || $socialId == '' || $source == '' || $type == '' ){
				throw new Exception( "Invalid embedCode/url, Please enter a valid link/embed !" );
			}
			$title = isset( $pageVars[ 'title' ] ) ? $pageVars[ 'title' ] : '';
			$title = makeUriTitle( $title );
			$description = isset( $pageVars[ 'description' ] ) ? $pageVars[ 'description' ] : '';
			$duration = isset( $pageVars[ 'duration' ] ) ? $pageVars[ 'duration' ] : '';
			$tags = isset( $pageVars[ 'tags' ] ) ? $pageVars[ 'tags' ] : '';
			
			$categoryId1 = isset( $pageVars[ 'categoryId1' ] ) ? $pageVars[ 'categoryId1' ] : 0;
			$categoryId2 = isset( $pageVars[ 'categoryId2' ] ) ? $pageVars[ 'categoryId2' ] : 0;

			$youtubeCategoryId = isset( $pageVars[ 'youtubeCategoryId' ] ) ? $pageVars[ 'youtubeCategoryId' ] : 0;

			$objMediaChk = getMultiple( 'media', "source||$source", "type||$type", "socialId||$socialId" );
			if( $objMediaChk->getResultCount() <= 0 ){
				// add data
				return getDb( 'media', 'add' )->set(
					"title||$title", "description||$description", "duration||$duration",
					"filePath||$filePath", "socialId||$socialId", "source||$source", "tags||$tags",
					"type||$type", 'status||published', "categoryId1||$categoryId1", 
					"categoryId2||$categoryId2", "youtubeCategoryId||$youtubeCategoryId" )->getLastInsertedId();
			}else{
				$objMediaChk->update( "title||$title", "description||$description", "duration||$duration",
					"filePath||$filePath", "tags||$tags", "categoryId1||$categoryId1", 
					"categoryId2||$categoryId2", "youtubeCategoryId||$youtubeCategoryId" );
				return $objMediaChk->getMediaId();
			}
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * Get and save file's EXIF Meta Details
	 * @param $usageId, $type, $url
	 * @return mediaMetadataId/false
	*/
	public static function saveFileExifData( $usageId, $type, $url ){
		try{
			$objMediaMetadata = getMultiple( 'mediaMetadata', "type||$type", "usageId||$usageId", 'count||Y' );
			if( $objMediaMetadata->getTotalCount() == 0 ){
				$exifData = getFileExifData( $url );
				if( count( $exifData ) > 0 ){
					$make = isset( $exifData[ 'Make' ] ) ? $exifData[ 'Make' ] : '';
					$model = isset( $exifData[ 'Model' ] ) ? $exifData[ 'Model' ] : '';
					$focalLength = isset( $exifData[ 'FocalLength' ] ) ? $exifData[ 'FocalLength' ] : '';
					$shutterSpeedValue = isset( $exifData[ 'ShutterSpeedValue' ] ) ? $exifData[ 'ShutterSpeedValue' ] : '';
					$apertureValue = isset( $exifData[ 'ApertureValue' ] ) ? $exifData[ 'ApertureValue' ] : '';
					$isoSpeedRatings = isset( $exifData[ 'ISOSpeedRatings' ] ) ? $exifData[ 'ISOSpeedRatings' ] : '';
					$dateTimeOriginal = isset( $exifData[ 'DateTimeOriginal' ] ) ? $exifData[ 'DateTimeOriginal' ] : '';
					$serialize = serialize( $exifData );
					// add the file metadata
					return getDb( 'mediaMetadata', 'add' )->set(
						"make||$make", "model||$model", 
						"focalLength||$focalLength", "shutterSpeedValue||$shutterSpeedValue", 
						"apertureValue||$apertureValue", "isoSpeedRatings||$isoSpeedRatings", 
						"dateTimeOriginal||$dateTimeOriginal", "serializeData||$serialize",
						"type||$type", "usageId||$usageId" )->getLastInsertedId();
				}
			}
			return false;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * Save uploaded image info 
	 * @param $pageVars 
	 * @return imagePath
	*/
	public static function saveTxtEditorImage( $pageVars ){
		try{
			global $eiLoginUserId;
			if( $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_array( $pageVars ) ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			if( count( $pageVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			// save file data
			$fileName = isset( $pageVars[ 'filename' ] ) ? makeUriTitle( $pageVars[ 'filename' ] ) : '';
			$imagePath = isset( $pageVars[ 'key' ] ) ? $pageVars[ 'key' ] : '';
			$mimetype = isset( $pageVars[ 'mimetype' ] ) ? $pageVars[ 'mimetype' ] : '';
			$size = isset( $pageVars[ 'size' ] ) ? $pageVars[ 'size' ] : 0;
			$width = isset( $pageVars[ 'width' ] ) ? $pageVars[ 'width' ] : 0;
			$height = isset( $pageVars[ 'height' ] ) ? $pageVars[ 'height' ] : 0;
			$tags = isset( $pageVars[ 'tags' ] ) ? $pageVars[ 'tags' ] : '';

			// check every parameters are required parameter			
			if( $fileName == '' || $imagePath == '' || $mimetype == '' || $size <= 0 || $width <= 0 || $height <= 0 ){
				throw new Exception( 'Invalid image parameter supplied !' );
			}
			// add user's linkedin data
			getDb( 'image', 'add' )->set( "title||$fileName", "uri||$imagePath", 
				"mimetype||$mimetype", "size||$size", "height||$height", 
				"width||$width", "tags||$tags" )->getLastInsertedId();
			return S3_IMAGE_PUBLIC_LOCATION . $imagePath;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * Save album media like
	 * @param $albumId, $albumMediaId
	 * @return $albumMediaLikeId
	*/
	public static function userAlbumMediaLikeSave( $albumId, $albumMediaId ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_numeric( $albumId ) || $albumId <= 0 || 
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			// check albumId existance
			if( getMultiple( 'album', "albumId||$albumId", 'count||Y' )->getTotalCount() <= 0 ){
				throw new Exception( "Invalid albumId parameter supplied !" );
			}
			// check albumMediaId existance
			$albumMediaObj = getMultiple( 'albumMedia', "albumMediaId||$albumMediaId" );
			if( $albumMediaObj->getResultCount() <= 0 ){
				throw new Exception( "Invalid albumMediaId parameter supplied !" );
			}
			// check for duplicate value
			if( getMultiple( 'albumMediaLike', "albumId||$albumId", 'companyId||0', "albumMediaId||$albumMediaId", 
				"createdUserId||$eiLoginUserId", 'count||Y' )->getTotalCount() ){
				return true;
			}
			$albumMediaLikeAddObj = getDb( 'albumMediaLike', 'add' );
			$albumMediaLikeAddObj->set( "albumId||$albumId", "albumMediaId||$albumMediaId",
				'companyId||0', 'type||' . $albumMediaObj->getType() );
			return $albumMediaLikeAddObj->getLastInsertedId();
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get album media user's total like
	 * @param $albumId, $albumMediaId
	 * @return total likes
	*/
	public static function userAlbumMediaLikeCount( $albumId, $albumMediaId ){
		try{
			if( !is_numeric( $albumId ) || $albumId <= 0 || 
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			// get total media likes
			return getMultiple( 'albumMediaLike', "albumId||$albumId", 
				"albumMediaId||$albumMediaId", 'companyId||0', 'count||Y' )->getTotalCount();
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * check if user album media is available for like
	 * @param $albumId, $albumMediaId
	 * @return true/false
	*/
	public static function canUserLikeAlbumMedia( $albumId, $albumMediaId ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				return false;
			}
			if( !is_numeric( $albumId ) || $albumId <= 0 ||
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 ){
				return false;
			}
			// get total media likes
			if( getMultiple( 'albumMediaLike', "albumId||$albumId", "albumMediaId||$albumMediaId", 
				"createdUserId||$eiLoginUserId", 'companyId||0', 'count||Y' )->getTotalCount() > 0 ){
				return false;
			}
			return true;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * Save company album media like
	 * @param $albumId, $albumMediaId, $companyId
	 * @return $albumMediaLikeId
	*/
	public static function companyAlbumMediaLikeSave( $albumId, $albumMediaId, $companyId ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_numeric( $albumId ) || $albumId <= 0 || 
				!is_numeric( $companyId ) || $companyId <= 0 ||
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			// check albumId existance
			if( getMultiple( 'album', "companyId||$companyId", "albumId||$albumId", 'count||Y' )->getTotalCount() <= 0 ){
				throw new Exception( "Invalid albumId parameter supplied !" );
			}
			// check albumMediaId existance
			$albumMediaObj = getMultiple( 'albumMedia', "companyId||$companyId", "albumMediaId||$albumMediaId" );
			if( $albumMediaObj->getResultCount() <= 0 ){
				throw new Exception( "Invalid albumMediaId parameter supplied !" );
			}
			// check for duplicate value
			if( getMultiple( 'albumMediaLike', "albumId||$albumId", "albumMediaId||$albumMediaId", 
				"companyId||$companyId", "createdUserId||$eiLoginUserId", 'count||Y' )->getTotalCount() ){
				return true;
			}
			$albumMediaLikeAddObj = getDb( 'albumMediaLike', 'add' );
			$albumMediaLikeAddObj->set( "albumId||$albumId", "companyId||$companyId", "albumMediaId||$albumMediaId",
				'type||' . $albumMediaObj->getType() );
			return $albumMediaLikeAddObj->getLastInsertedId();
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get company album media total like
	 * @param $albumId, $albumMediaId, $companyId
	 * @return total likes
	*/
	public static function companyAlbumMediaLikeCount( $albumId, $albumMediaId, $companyId ){
		try{
			if( !is_numeric( $albumId ) || $albumId <= 0 || 
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 || 
				!is_numeric( $companyId ) || $companyId <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			// get total media likes
			return getMultiple( 'albumMediaLike', "albumId||$albumId", "companyId||$companyId",
				"albumMediaId||$albumMediaId", 'count||Y' )->getTotalCount();
		}catch( Exception $ex ){
			throw $ex;
		}
	}
	
	
	public static function companyAlbumMediaLikePeopleName( $albumId, $albumMediaId, $companyId ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $albumId ) || $albumId <= 0 ||
					!is_numeric( $albumMediaId ) || $albumMediaId <= 0 ||
					!is_numeric( $companyId ) || $companyId <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$albumMediaLike = array();
			
			$albumMediaLikeObjTotalCount = getMultiple('albumMediaLike', "albumId||$albumId", "companyId||$companyId", "albumMediaId||$albumMediaId", 'count||Y' )->getTotalCount();
			
			$albumMediaLikeObjTemp = getMultiple('albumMediaLike', "albumId||$albumId", "companyId||$companyId", "albumMediaId||$albumMediaId", "createdUserId||$eiLoginUserId", 'count||Y' )->getTotalCount();
			if($albumMediaLikeObjTemp>0){
				$albumMediaLike[] = 'You';
			}
			
			$albumMediaLikeObjs = getMultiple('albumMediaLike', "albumId||$albumId", "companyId||$companyId", "albumMediaId||$albumMediaId", "quantity||2");
			foreach($albumMediaLikeObjs() as $albumMediaLikeObj){
				$userId = $albumMediaLikeObj->getCreatedUserId();
				if($userId!=$eiLoginUserId){
					$userObj = getSingle('user', $userId);
					$albumMediaLike[] = $userObj->getLogin();
				}
				
				$comma_separated = implode(", ", $albumMediaLike);
			}
			$albumMediaLikeArrayCount = count($albumMediaLike);
			$otherCount = $albumMediaLikeObjTotalCount -$albumMediaLikeArrayCount;
			if($otherCount>0){
				$albumMediaLikeMessage = $comma_separated.' and '.$otherCount.' others ';
			}else{
				$albumMediaLikeMessage = $comma_separated;
			}
			return $albumMediaLikeMessage;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * check if company album media is available for like
	 * @param $albumId, $albumMediaId, $companyId
	 * @return true/false
	*/
	public static function canCompanyLikeAlbumMedia( $albumId, $albumMediaId, $companyId ){
		
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				return false;
			}
			if( !is_numeric( $albumId ) || $albumId <= 0 || 
				!is_numeric( $albumMediaId ) || $albumMediaId <= 0 || 
				!is_numeric( $companyId ) || $companyId <= 0 ){
				return false;
			}
			// get total media likes
			if( getMultiple( 'albumMediaLike', "albumId||$albumId", 
				"companyId||$companyId", "albumMediaId||$albumMediaId", 
				"createdUserId||$eiLoginUserId", 'count||Y' )->getTotalCount() > 0 ){
				return false;
			}
			return true;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/* MEDIA CENTER AREA */

	/*
	 * Save media center data
	 * @param $postVars
	 * @return mediaCenterId
	*/
	public static function saveMediaCenterData( $postVars ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( count( $postVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$usageId = isset( $postVars[ 'usageId' ] ) ? trim( $postVars[ 'usageId' ] ) : 0;
			if( !is_numeric( $usageId ) || $usageId <= 0 ){
				throw new Exception( 'usageId is a required parameter !' );
			}
			$usageType = isset( $postVars[ 'usageType' ] ) ? trim( $postVars[ 'usageType' ] ) : '';
			if( $usageType == '' ){
				throw new Exception( 'usageType is a required parameter !' );
			}
			$title = isset( $postVars[ 'title' ] ) ? trim( $postVars[ 'title' ] ) : '';
			if( $title == '' ){
				throw new Exception( 'title is a required parameter !' );
			}

			$description = isset( $postVars[ 'description' ] ) ? trim( $postVars[ 'description' ] ) : '';
			$link = isset( $postVars[ 'link' ] ) ? trim( $postVars[ 'link' ] ) : '';
			$date = isset( $postVars[ 'date' ] ) ? trim( $postVars[ 'date' ] ) : '';

			$type = isset( $postVars[ 'type' ] ) ? trim( $postVars[ 'type' ] ) : '';
			if( $type == '' ){
				throw new Exception( 'media center type is a required parameter !' );
			}
			if( $type == 'news' ){
				if( $description == '' ){
					throw new Exception( 'description is a required parameter !' );
				}
			}elseif( $type == 'release' ){
				if( $date == '' ){
					throw new Exception( 'date is a required parameter !' );
				}
			}elseif( $type == 'citation' ){
				if( $description == '' ){
					throw new Exception( 'description is a required parameter !' );
				}
				if( $link == '' ){
					throw new Exception( 'link is a required parameter !' );
				}
				if( $date == '' ){
					throw new Exception( 'date is a required parameter !' );
				}
			}

			$fileType = '';
			$fileId = 0;
			$fileData = isset( $postVars[ 'fileData' ] ) ? trim( $postVars[ 'fileData' ] ) : '';
			if( $type != 'news' )
			{
				if( $fileData == '' ){
					throw new Exception( 'Media center file is a required parameter !' );
				}
			}
			
			if( $fileData != '' ){
				$fileDataArr = json_decode( $fileData, true );
				if( count( $fileDataArr ) <= 0 ){
					throw new Exception( 'Invalid media file data, Please try another !' );
				}
				$fileMimtypeArr = explode( '/' , $fileDataArr[ 'mimetype' ] );
				$fileType = $fileMimtypeArr[ 0 ] == 'image' ? 'image' : 'media';
				// add file to media table first 
				$fileId = MediaUtility::saveMediaData( $fileDataArr );
				if( $fileId <= 0 ){
					throw new Exception( 'Unable tp add media file data, Please try later !' );
				}
			}

			// check for duplicate value
			$mediaCenterChk = getMultiple( 'mediaCenter', "usageId||$usageId", 
				"usageType||$usageType", "type||$type", "title||$title" ,'count||Y' )->getTotalCount();
			if( $mediaCenterChk > 0 ){
				throw new Exception( 'Title already exist please try another !' );
			}

			$date = $date != '' ? date( 'Y-m-d', strtotime( $date ) ) : '';
			// add table
			$mediaCenterId = getDb( 'mediaCenter', 'add' )->set(
				"usageId||$usageId", "usageType||$usageType", "type||$type",
				"title||$title", "description||$description", "link||$link", "date||$date" 
			)->getLastInsertedId();

			// save media center file data
			if( $type != 'news' )
			{
				if( $mediaCenterId <= 0 || $fileId <= 0 || $fileType == '' ){
					throw new Exception( "Media center file is a required parameter !" );
				}
			}
			self::saveMediaCenterFileData( $mediaCenterId, $fileId, $fileType, $type );
			return $mediaCenterId;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/*
	 * Save media center data
	 * @param $postVars
	 * @return mediaCenterId
	*/
	public static function editMediaCenterData( $postVars ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( count( $postVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$mediaCenterId = isset( $postVars[ 'mediaCenterId' ] ) ? trim( $postVars[ 'mediaCenterId' ] ) : 0;
			if( !is_numeric( $mediaCenterId ) || $mediaCenterId <= 0 ){
				throw new Exception( 'mediaCenterId is a required parameter !' );
			}

			$objMediaCenterEdit = getSingle( 'mediaCenter', $mediaCenterId );
			if( $objMediaCenterEdit->getResultCount() <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}

			$title = isset( $postVars[ 'title' ] ) ? trim( $postVars[ 'title' ] ) : '';
			if( $title == '' ){
				throw new Exception( 'title is a required parameter !' );
			}

			$description = isset( $postVars[ 'description' ] ) ? trim( $postVars[ 'description' ] ) : '';
			$link = isset( $postVars[ 'link' ] ) ? trim( $postVars[ 'link' ] ) : '';
			$date = isset( $postVars[ 'date' ] ) ? trim( $postVars[ 'date' ] ) : '';

			$type = $objMediaCenterEdit->getType();
			if( $type == '' ){
				throw new Exception( 'media center type is a required parameter !' );
			}
			if( $type == 'news' ){
				if( $description == '' ){
					throw new Exception( 'description is a required parameter !' );
				}
			}elseif( $type == 'release' ){
				if( $date == '' ){
					throw new Exception( 'date is a required parameter !' );
				}
			}elseif( $type == 'citation' ){
				if( $description == '' ){
					throw new Exception( 'description is a required parameter !' );
				}
				if( $link == '' ){
					throw new Exception( 'link is a required parameter !' );
				}
				if( $date == '' ){
					throw new Exception( 'date is a required parameter !' );
				}
			}
			$date = $date != '' ? date( 'Y-m-d', strtotime( $date ) ) : '';


			$oldFileId = isset( $postVars[ 'fileId' ] ) ? trim( $postVars[ 'fileId' ] ) : 0;
			$fileData = isset( $postVars[ 'fileData' ] ) ? trim( $postVars[ 'fileData' ] ) : '';
			
			// edit table data
			$objMediaCenterEdit->update( "title||$title", "description||$description", "link||$link", "date||$date" );

			$fileType = '';
			$newFileId = 0;
			if( $fileData != '' ){
				$fileDataArr = json_decode( $fileData, true );
				if( count( $fileDataArr ) <= 0 ){
					throw new Exception( 'Invalid media file data, Please try another !' );
				}
				$fileMimtypeArr = explode( '/' , $fileDataArr[ 'mimetype' ] );
				$fileType = $fileMimtypeArr[ 0 ] == 'image' ? 'image' : 'media';
				// add file to media table first 
				$newFileId = MediaUtility::saveMediaData( $fileDataArr );
				if( $newFileId <= 0 ){
					throw new Exception( 'Unable tp add media file data, Please try later !' );
				}
			}
			if( $newFileId > 0 && $fileType != '' ){
				// remove all old entries for media center id
				getDb( 'mediaCenterFile', 'delete' )->set( "whereClause||mediaCenterId=$mediaCenterId" );
				// save media center file data
				self::saveMediaCenterFileData( $mediaCenterId, $newFileId, $fileType );
			}
			return $mediaCenterId;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/*
	 * Save media center file data
	 * @param $postVars
	 * @return mediaCenterFileId
	*/
	public static function saveMediaCenterFileData( $mediaCenterId, $fileId, $fileType, $type ){
		try{
			if( !is_numeric( $mediaCenterId ) || $mediaCenterId <= 0 ){
				throw new Exception( 'Unable to add media center file !' );
			}
			if($type!='news')
			{
				if( !is_numeric( $fileId ) || $fileId <= 0 ){
					throw new Exception( 'fileId is required parameter, Unable to add media center file !' );
				}
				if( $fileType == '' ){
					throw new Exception( 'fileType is required parameter, Unable to add media center file !' );
				}
			}
			
			// check for duplicate value
			$objMediaCenterFileChk = getMultiple( 'mediaCenterFile', "mediaCenterId||$mediaCenterId", 
				"usageId||$fileId", "type||$fileType", 'count||Y' )->getTotalCount();
			if( $objMediaCenterFileChk > 0 ){
				return true;
			}
			// add table
			return getDb( 'mediaCenterFile', 'add' )->set(
				"mediaCenterId||$mediaCenterId", "usageId||$fileId", "type||$fileType" )->getLastInsertedId();
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * get media center element details for edit
	 * @param $mediaCenterId
	 * @return array
	*/
	public static function getMediaCenterData( $mediaCenterId ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( !is_numeric( $mediaCenterId ) || $mediaCenterId <= 0 ){
				throw new Exception( 'mediaCenterId is a required parameter !' );
			}
			$objMediaCenter = getMultiple( 'mediaCenter', "mediaCenterId||$mediaCenterId" );
			if( $objMediaCenter->getResultCount() <= 0 ){
				throw new Exception( 'No data found !' );
			}
			$objMediaCenterFile = getMultiple( 'mediaCenterFile', "mediaCenterId||$mediaCenterId" );
			if( $objMediaCenterFile->getResultCount() <= 0 ){
				throw new Exception( 'No file data found !' );
			}

			$filePath = $fileType = '';
			$mediaCenterFileId = $objMediaCenterFile->getMediaCenterFileId();
			if( $objMediaCenterFile->getType() == 'image' ){
				$objImage = getSingle( 'image', $objMediaCenterFile->getUsageId() );
				if( $objImage->getResultCount() > 0 ){
					$filePath = S3_IMAGE_PUBLIC_LOCATION . $objImage->getUri();
					$fileType = 'image';
				}
			}elseif( $objMediaCenterFile->getType() == 'media' ){
				$objMedia = getSingle( 'media', $objMediaCenterFile->getUsageId() );
				if( $objMedia->getResultCount() > 0 ){
					$filePath = S3_IMAGE_PUBLIC_LOCATION . $objMedia->getFilePath();
					$fileType = $objMedia->getType();
				}
			}
// 			if( $filePath == '' ){
// 				throw new Exception( 'No file data found !' );
// 			}
			$fileName = array_pop( explode( '/', $filePath ) );
			return array(
				'id' => $mediaCenterId, 'type' => $objMediaCenter->getType(),
				'title' => $objMediaCenter->getTitle(), 'description' => $objMediaCenter->getDescription(),
				'link' => $objMediaCenter->getLink(), 'mediaCenterFileId' => $mediaCenterFileId, 
				'filePath' => $filePath, 'fileName' => $fileName, 'fileType' => $fileType, 
				'date' => $objMediaCenter->getDate() != '' ? date( 'd-m-Y', strtotime( $objMediaCenter->getDate() ) ) : ''
			);
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/*
	 * Get media center home page top nav bar with total count
	 * @param $usageType, $usageId, $selectedNav
	 * @return array
	*/
	public static function getMediaCenterTopNav( $usageType, $usageId, $selectedNav='' ){
		try{
			$mediaTypes = array( 'news' => 0, 'release' => 0, 'citation' => 0, 'download' => 0 );
			$objMediaCenters = getDb( 'mediaCenter' )->set( 'groupBy||type', 'columns||type', "usageType||$usageType", "usageId||$usageId" );
			$mediaTypeCounts = $objMediaCenters->getRecords();
			$mediaTypesArr = array();
			foreach( $mediaTypeCounts as $mediaTypeCount ){
				$mediaTypesArr[ $mediaTypeCount[ 'type' ] ] = $mediaTypeCount[ 'groupByCount' ];
			}
			$selectedNav = isset( Url::$pageVars[ 'GETPOST' ][ 'media' ] ) ? Url::$pageVars[ 'GETPOST' ][ 'media' ] : '';
			$mediaCenterNav = array();
			$mediaCenterNav[] = array( 
				'text' => 'ALL', 'link' => HOME_PATH . "press/",  'active' => $type == $selectedNav ? 'active nbg1' : '' 
			);
			foreach( $mediaTypes as $type => $mediaType ){
				$totalCount = isset( $mediaTypesArr[ $type ] ) ? $mediaTypesArr[ $type ] : 0;
				if( $totalCount <= 0 ){
					continue;
				}
				$text = $type == 'news' ? $type : $type . 's';
				$mediaCenterNav[] = array( 
					'text' => strtoupper( $text ) . " ($totalCount)", 'link' => HOME_PATH . "press/nv/media/$type/", 
					'active' => $type == $selectedNav ? 'active nbg1' : '' 
				);
			}
			return $mediaCenterNav;
		}catch( Exception $ex ){
			throw $ex;
		}
	}
	
	
	/*
	 * Get media center home page top nav bar with total count
	 * @param $usageType, $usageId, $selectedNav
	 * @return array
	 */
	public static function getMediaCenterTopNavHome( $usageType, $usageId, $selectedNav='' ){
		try{
			$mediaTypes = array( 'news' => 0, 'release' => 0, 'citation' => 0, 'download' => 0 );
			$objMediaCenters = getDb( 'mediaCenter' )->set( 'groupBy||type', 'columns||type', "usageType||$usageType", "usageId||$usageId" );
			$mediaTypeCounts = $objMediaCenters->getRecords();
			$mediaTypesArr = array();
			foreach( $mediaTypeCounts as $mediaTypeCount ){
				$mediaTypesArr[ $mediaTypeCount[ 'type' ] ] = $mediaTypeCount[ 'groupByCount' ];
			}
			$selectedNav = isset( Url::$pageVars[ 'GETPOST' ][ 'media' ] ) ? Url::$pageVars[ 'GETPOST' ][ 'media' ] : '';
			$mediaCenterNav = array();
			$mediaCenterNav[] = array(
					'text' => 'ALL', 'link' => HOME_PATH . "about/",  'active' => $type == $selectedNav ? 'active nbg1' : ''
			);
			foreach( $mediaTypes as $type => $mediaType ){
				$totalCount = isset( $mediaTypesArr[ $type ] ) ? $mediaTypesArr[ $type ] : 0;
				if( $totalCount <= 0 ){
					continue;
				}
				$text = $type == 'news' ? $type : $type . 's';
				$mediaCenterNav[] = array(
						'text' => strtoupper( $text ) . "($totalCount)", 'link' => HOME_PATH . "about/nv/media/$type/",
						'active' => $type == $selectedNav ? 'active nbg1' : ''
				);
			}
			return $mediaCenterNav;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	
	/*
	 * Get media center data for home page
	 * @param $usageType, $usageId, $selectedNav
	 * @return array
	*/
	public static function getMediaCenterElements( $usageType, $usageId, $selectedNav='', $pageNumber=1, $quantity=4 ){
		try{
			if( !is_numeric( $usageId ) || $usageId <= 0 || $usageType == '' ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$whereClause = array( 'status||active', "usageType||$usageType", "usageId||$usageId", 'paging||Y', "quantity||$quantity", "pageNumber||$pageNumber" );
			if( $selectedNav != '' ){
				$whereClause[] = "type||$selectedNav";
			}
			$fileClass = '';
			$elements = array();
			$objMediaCenters = getMultiple( 'mediaCenter', $whereClause );
			foreach( $objMediaCenters() as $objMediaCenter ){
				$mediaCenterId = $objMediaCenter->getMediaCenterId();
				$objMediaCenterFile = getMultiple( 'mediaCenterFile', "mediaCenterId||$mediaCenterId" );
				if( $objMediaCenterFile->getResultCount() <= 0 ){
					continue;
				}
				$fileType = $objMediaCenterFile->getType();
				$imagePath = $docType = $fileTitle = '';
				$dLink = ABSOLUTE_HOME_PATH . "download/nv/type/mediacenter/id/$mediaCenterId/element/$fileType/";
				if( $fileType == 'image' ){
					$objImage = getSingle( 'image', $objMediaCenterFile->getUsageId() );
					if( $objImage->getResultCount() <= 0 ){
						continue;
					}
					$imagePath = ImageUtility::getScaledImage( $objImage->getUri(), 170, 140 );
					$fileTitle = $objImage->getTitle();
					$docType = 'image';
				}else{
					$objMedia = getSingle( 'media', $objMediaCenterFile->getUsageId() );
					if( $objMedia->getResultCount() <= 0 ){
						$imagePath = '/images/defaultCompanyImg.jpg';
					}
					$fileTitle = $objMedia->getTitle();
					$docType = $objMedia->getType();
					$fileClass = getCssFileClassByExtension( $objMedia->getType() );
				}
				$elements[] = array(
					'id' => $mediaCenterId, 'pubLink' => $objMediaCenter->getLink(), 'type' => $objMediaCenter->getType(),
					'imagePath' => $imagePath, 'title' => substr( $objMediaCenter->getTitle(), 0,60), 
					'fileType' => $fileType, 'dLink' => $dLink, 'desc' => substr( $objMediaCenter->getDescription(), 0, 40 ),
					'fileTitle' => $fileTitle, 'docType' => $docType, 'fileClass' => $fileClass,
					'time' => date( 'j M Y', strtotime( $objMediaCenter->getcreatedTime() ) ),
					'date' => date( 'j M Y', strtotime( $objMediaCenter->getDate() ) )
				);
			}
			return $elements;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * Remove media center element
	 * @param $postVars
	 * @return true/false
	*/
	public static function removeMediaCenterData( $postVars ){
		try{
			global $eiLoginUserId;
			if( !is_numeric( $eiLoginUserId ) || $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			if( count( $postVars ) <= 0 ){
				throw new Exception( 'Invalid parameter supplied !' );
			}
			$mediaCenterId = isset( $postVars[ 'mediaCenterId' ] ) ? trim( $postVars[ 'mediaCenterId' ] ) : 0;
			if( !is_numeric( $mediaCenterId ) || $mediaCenterId <= 0 ){
				throw new Exception( 'usageId is a required parameter !' );
			}
			$usageId = isset( $postVars[ 'usageId' ] ) ? trim( $postVars[ 'usageId' ] ) : 0;
			if( !is_numeric( $usageId ) || $usageId <= 0 ){
				throw new Exception( 'usageId is a required parameter !' );
			}
			$usageType = isset( $postVars[ 'usageType' ] ) ? trim( $postVars[ 'usageType' ] ) : '';
			if( $usageType == '' ){
				throw new Exception( 'usageType is a required parameter !' );
			}
			if( getMultiple( 'mediaCenter', "mediaCenterId||$mediaCenterId", 
				"usageId||$usageId", "usageType||$usageType", 'count||Y' )->getTotalCount() > 0 ){
				// remove data
				getDb( 'mediaCenter', 'delete' )->set( "whereClause||mediaCenterId=$mediaCenterId and usageId=$usageId and usageType='$usageType'" );
				getDb( 'mediaCenterFile', 'delete' )->set( "whereClause||mediaCenterId=$mediaCenterId" );
			}
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/* eof MEDIA CENTER AREA */


}
